package cc.mango.common;

import java.util.List;
import java.util.Stack;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import cc.mango.android.dto.StkDaily;
import cc.mango.android.dto.StkIntraday;
import cc.mango.data.StkDailyData;
import cc.mango.data.StkIntradayData;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

//any time there's only one  task on  DownLoadChartDataAnsycTask;
public class DownLoadChartDataAnsycTask extends AsyncTask<String, Void, Void>
{
	private static String						TAG					= DownLoadChartDataAnsycTask.class
																		.getSimpleName();
	private static Stack<DownLoadChartDataAnsycTask>	_taskList				= new Stack<DownLoadChartDataAnsycTask>();
	private static ProgressDialog					dialog				= null;

	private Context							_context;
	protected static String						_progressTitle_res		= null;
	protected static String						_progressContent_res	= null;
	protected Runnable							_updateUI;
	private String								_stockCode;
	private String								_dataType;
	private StkIntradayData						_fetchStkIntraDayData;
	private static List<StkIntraday>				_listOnStkIntraday;
	private List<StkDaily>						_listOnStkDaily;
	private StkDailyData						_fetchStkDailyData;
	private final Object						_mLock				= new Object();

	public DownLoadChartDataAnsycTask(StkIntradayData fetch, Context context,
			Runnable updateUI)
	{
		_context = context;
		dialog = new ProgressDialog(_context);
		_updateUI = updateUI;
		_progressTitle_res = _context.getResources().getString(
				R.string.loading_indicator);
		_progressContent_res = _context.getResources().getString(
				R.string.loading_mesgr);
		_fetchStkIntraDayData = fetch;
		while (!_taskList.isEmpty())
		{
			DownLoadChartDataAnsycTask job = _taskList.pop();
			job.cancel(true);
		}
		_taskList.add(this);
	}

	public DownLoadChartDataAnsycTask(StkDailyData fetch, Context context,
			Runnable updateUI)
	{
		_context = context;
		dialog = new ProgressDialog(_context);
		_updateUI = updateUI;
		_progressTitle_res = _context.getResources().getString(
				R.string.loading_indicator);
		_progressContent_res = _context.getResources().getString(
				R.string.loading_mesgr);
		_fetchStkDailyData = fetch;
		while (!_taskList.isEmpty())
		{
			DownLoadChartDataAnsycTask job = _taskList.pop();
			job.cancel(true);
		}
		_taskList.add(this);
	}

	String getStockCode()
	{
		return _stockCode;
	}

	@Override
	protected void onCancelled()
	{
		// TODO Auto-generated method stub
		super.onCancelled();
		if (dialog != null && dialog.isShowing())
		{
			dialog.dismiss();
		}

	}

	String getDataType()
	{
		return _dataType;
	}

	final public synchronized List<StkIntraday> getListOnIntraday()
	{
		return _listOnStkIntraday;
	}

	final public synchronized List<StkDaily> getListOnDaily()
	{
		return _listOnStkDaily;
	}

	@Override
	protected Void doInBackground(String... params)
	{
		_stockCode = params[0];
		_dataType = params[1];
		TDStockLog.d(TAG, "stockCode =" + _stockCode);
		TDStockLog.d(TAG, "dataType =" + _dataType);
		if (_fetchStkIntraDayData != null)
		{
			try
			{
				_listOnStkIntraday = _fetchStkIntraDayData.download(
						getStockCode(), getDataType());
				TDStockLog.d(TAG, "finished download, and its size is : "
						+ _listOnStkIntraday.size());
			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				TDStockLog.d( TAG,
							e.getMessage() == null ? "unknow exception happened!"
										: e.getMessage());
			}

		}
		else if (_fetchStkDailyData != null)
		{
			try
			{
				_listOnStkDaily = _fetchStkDailyData.download(
						getStockCode(), getDataType());
				TDStockLog.d(TAG, "finished download, and its size is : "
						+ _listOnStkDaily.size());
			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				TDStockLog.d( TAG,
							e.getMessage() == null ? "unknow exception happened!"
										: e.getMessage());
			}

		}
		return null;
	}

	@Override
	protected void onPostExecute(Void result)
	{
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		TDStockLog.d(TAG, "ready to update UI!");

		try
		{

			if (dialog != null && dialog.isShowing())
			{
				TDStockLog.d(TAG, "ready to hide dialog!");
				dialog.dismiss();
				TDStockLog.d(TAG, "hide dialog completely!");
			}
			if (this.isCancelled())
			{
				return;
			}
			if (_updateUI != null && !this.isCancelled())
			{
				TDStockLog.d(TAG, "begin to update UI!");
				_updateUI.run();
				TDStockLog.d(TAG, " update UI completely!");
			}
			else
			{
				TDStockLog.d(TAG, _updateUI == null ? " updateUI = null!"
						: "updateUI is not null");
				TDStockLog.d(TAG, this.isCancelled() ? " this.isCancelled!"
						: "this.is not Cancelled!");
			}
		}
		catch (Exception e)
		{
			TDStockLog.d(TAG, e.getMessage() == null ? "unknow exception happened!"
					: e.getMessage());
		}
	}

	@Override
	protected void onPreExecute()
	{
		// TODO Auto-generated method stub
		super.onPreExecute();
		dialog.setTitle(_progressTitle_res);
		dialog.setMessage(_progressContent_res);
		dialog.show();

	}

}
